﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Liang.Util
{
    /// <summary>
    /// 字节处理类
    /// </summary>
    public static class Bytes
    {
        /// <summary>
        /// 将对象转换为字节数组
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] Object2Bytes(object obj)
        {
            byte[] buff = new byte[Marshal.SizeOf(obj)];
            try
            {
                IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
                Marshal.StructureToPtr(obj, ptr, true);
            }
            catch (Exception e)
            {
                throw e;
            }

            return buff;
        }

        /// <summary>
        /// 将字节数组转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static object Bytes2Object<T>(byte[] buff)
        {
            IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
            return Marshal.PtrToStructure(ptr, typeof(T));
        }

        /// <summary>
        /// 字节数组转换为16进制字符串
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <returns>16进制字符串</returns>
        public static string ByteArrayToHexString(Byte[] b)
        {
            string strHexString = string.Empty;

            for (int i = 0; i < b.Length; i++)
            {
                strHexString += b[i].ToString("X2");
            }

            return strHexString;
        }

        /// <summary>
        /// 字节数组转换为16进制字符串
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <param name="strSpl">分隔符</param>
        /// <returns>16进制字符串</returns>
        public static string ByteArrayToHexString(Byte[] b, string strSpl)
        {
            string strHexString = string.Empty;

            for (int i = 0; i < b.Length; i++)
            {
                if (i != b.Length - 1)
                {
                    // 不是最后一项
                    strHexString += b[i].ToString("X2") + strSpl;
                }
                else
                {
                    strHexString += b[i].ToString("X2");
                }
            }

            return strHexString;
        }

        /// <summary>
        /// 16进制字符串转换为字节数组
        /// </summary>
        /// <param name="strHexString">16进制字符串</param>
        /// <returns>字节数组</returns>
        public static Byte[] HexStringToByteArray(string strHexString)
        {
            string[] strSplit = strHexString.Split(new char[] { ' ' });

            Byte[] b = new Byte[strSplit.Length];

            for (int i = 0; i < strSplit.Length; i++)
            {
                b[i] = Convert.ToByte(strSplit[i], 16);
            }

            return b;
        }


        /// <summary>
        /// 16进制字符串转换为字节数组
        /// </summary>
        /// <param name="strHexString">16进制字符串</param>
        /// <param name="strSpl">分隔符char[]</param>
        /// <returns>字节数组</returns>
        public static Byte[] HexStringToByteArray(string strHexString, char[] strSpl)
        {
            string[] strSplit = strHexString.Split(strSpl);

            Byte[] b = new Byte[strSplit.Length];

            for (int i = 0; i < strSplit.Length; i++)
            {
                b[i] = Convert.ToByte(strSplit[i].Trim(), 16);
            }

            return b;
        }

        /// <summary>
        /// 获取高字节
        /// </summary>
        /// <param name="intval"></param>
        /// <returns></returns>
        public static byte GetHighByte(short intval)
        {
            return (byte)(intval >> 8);
        }

        /// <summary>
        /// 获取低字节
        /// </summary>
        /// <param name="intval"></param>
        /// <returns></returns>
        public static byte GetLowByte(short intval)
        {
            return (byte)intval;
        }

        public static short MakeShort(byte high, byte low)
        {
            short tmpHigh = (short)(high << 8);
            short tmpLow = low;

            return (short)(tmpHigh | tmpLow);
        }
    }
}
